package org.jeecg.glr.job;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;
import org.jeecg.glr.api.constant.ApiConstant;
import org.jeecg.glr.service.IGlrLightCurrentService;
import org.jeecg.glr.service.IGlrLightDataService;
import org.jeecg.glr.service.IGlrVideoYsService;
import org.jeecg.glr.util.StringUtil;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 路灯网关统计数据统一处理
 * 定时任务
 */
@Slf4j
public class LightDataJob implements Job {

    @Value("${spring.quartz.job.lightdata_is_runing}")
    private String lightdata_is_runing;

    @Autowired
    private IGlrLightDataService glrLightDataService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        long start = System.currentTimeMillis();
        String stJobName="路灯网关统计";
        log.info(String.format("1处理"+stJobName+"定时任务开始!===时间:" + DateUtils.getTimestamp()));
        try{
            if (!"Y".equals(lightdata_is_runing.toUpperCase())){
                log.info("2===================处理"+stJobName+"定时任务取消===================");
                return;
            }

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date d2  = new Date();

            if (false){
                //计算一段时间

                Date d1 = null;
                try {
                    // 注意格式需要与上面一致，不然会出现异常
                    d1 = sdf.parse(" 2020-05-13");
                } catch (ParseException e) {
                    e.printStackTrace();
                }

                Date tmp = d1;
                Calendar dd = Calendar.getInstance();
                dd.setTime(d1);

                // 打印2018年2月25日到2018年3月5日的日期
                while (tmp.getTime() < d2.getTime()) {
                    tmp = dd.getTime();

                    String stDate=sdf.format(dd.getTime());
                    log.info(stDate);
                    glrLightDataService.doSaveDate(stDate);
                    // 天数加上1
                    dd.add(Calendar.DAY_OF_MONTH, 1);
                }
            }else{

                Calendar calendar = Calendar.getInstance();
                calendar.setTime(d2);
                calendar.add(calendar.DATE, -1);
                String stDate=sdf.format(calendar.getTime());
                log.info(stDate);
                glrLightDataService.doSaveDate(stDate);
            }

            log.info("3===================处理"+stJobName+"定时任务结束===================");

        }catch (Exception e){
            e.printStackTrace();
            log.info(e.getMessage());
            log.info("4===================ERROR处理"+stJobName+"定时务错误===================");
        }
        log.info(String.format("5处理"+stJobName+"定时任务结束!===时间:" + DateUtils.getTimestamp()));
        long end = System.currentTimeMillis();
        long times = end - start;
        log.info("6处理"+stJobName+"总耗时" + times + "毫秒");
    }

}
